<?php
/**
 * alltosun.com 公共函数库 functions.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 勾国印 (gougy@alltosun.com) $
 * $Date: 2014-10-20 下午5:20:19 $
 * $Id$
*/

/**
 * 显示距离当前时间的字符串
 * @param $time 时间戳
 * @return string
 */
function tranTime($time)
{
    $rtime = date("m-d H:i",$time);
    $htime = date("H:i",$time);

    $time = time() - $time;

    if ($time < 60)
    {
        $str = '刚刚';
    }
    elseif ($time < 60 * 60)
    {
        $min = floor($time/60);
        $str = $min.'分钟前';
    }
    elseif ($time < 60 * 60 * 24)
    {
        $h = floor($time/(60*60));
        $str = $h.'小时前 '.$htime;
    }
    elseif ($time < 60 * 60 * 24 * 3)
    {
        $d = floor($time/(60*60*24));
        if($d==1)
            $str = '昨天 '.$rtime;
        else
            $str = '前天 '.$rtime;
    }
    else
    {
        $str = $rtime;
    }
    return $str;
}

/**
 * 返回配置项对应值
 * @param string|integer $field 标识名,标识为空则返回所有配置项数组
 * @param string|integer $config_type 配置类型
 * @return string
 */
function get_config_value($field = '', $type = 'site') {
    $System = M('System');
    if ($field) {
        $value = $System->where(array('name' => $field))->getField('value');
        //select value from go_system where name=$field
        return $value;
    } else {
        $config_list = $System->select();
        //select * from go_system
        return $config_list;
    }

}

//文件上传方法
/*$buttonname:上传按钮name,*/
function upload($buttonname,$updir,$twidth,$theight) {
    $tmp_name=$_FILES[$buttonname]['tmp_name'];
    if(file_exists($tmp_name)){
        import('ORG.Net.UploadFile');
        // 实例化上传类
        $upload = new UploadFile();
        // 设置附件上传大小
        $upload->maxSize  = 3145728 ;
        // 设置附件上传类型
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
        // 设置附件上传目录
        $uploaddir=$upload->savePath =  './Uploads/'.$updir.'/';
        //开启缩略图
        $upload->thumb=ture;
        $upload->thumbMaxWidth=$twidth;
        $upload->thumbMaxHeight=$theight;
        if(!$upload->upload()) {// 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            $info =  $upload->getUploadFileInfo();
            $url=$uploaddir.$info[0]['savename'];
            $url=ltrim($url,'./');
            return $url;
        }
    }

}
function get_list($model, $order = 'Id desc')
{
    $Model = M($model);
    $model_list = $Model->order($order)->select();
    //select * from go_$Model order by $order
    return $model_list;

}

//获取学生头像列表
function get_student_img($std_id)
{
    $Img = M('Image');
    $student_image = $Img->where(array('std_id' => $std_id))->getField('url');
    //select url from go_image where std_id=$std_id
    if(strlen($student_image)==0)
    {
        $student_image=__ROOT__."/Public/Images/user.jpg";
    }
    return $student_image;
}

//获取名字
function get_title($model, $id)
{
    $title = M($model)->where(array('Id' => $id))->getField('name');
    //select name from go_$model where Id=$id
    if (is_bool($title) || $title == '') {
        $title = "其他";
    }
    return $title;
}

//检测名称是否存在
function check_title($model, $title)
{
    $info = M($model)->where(array('name' => $title))->find();
    //select * from go_$model where name=$name
    if($info){
        return true;
    }else{
        return false;
    }
}
//检测所在名次
function get_mc($std_id,$lesson_id)
{
    $Model = new \Think\Model();
    $mc=$Model->query("select count(*) from go_score where score>(select score from go_score where std_id=".$std_id." and lesson_id=".$lesson_id.") and lesson_id=".$lesson_id." order by score desc");
    return ((int)$mc+1);
}
//检测是否评估过课程
function check_assess($lesson_id)
{
    $info = M("Assess")->where(array('lesson_id' => $lesson_id))->getField("grade");
    //select * from go_$model where name=$name
    if($info!=0){
        return $info;
    }else{
        return 0;
    }
}